/*
 * Copyright (c) 2009-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <math.h>
#include "alkes/gfx/IRenderTarget3D.h"
#include "alkes/gfx/IGraphics3D.h"
#include "alkes/gfx/LineShape3DImpl.h"
#include "alkes/gfx/RectangleShape3DImpl.h"
#include "alkes/gfx/ImageShape3DImpl.h"
#include "alkes/gfx/PolylineShape3DImpl.h"

namespace alkes {

IRenderTarget3D::IRenderTarget3D(IGraphics3D* owner, TargetBehavior target_behavior)
: IRenderTarget2D(owner, target_behavior)
{
}

IRenderTarget3D::~IRenderTarget3D()
{
}

IGraphics3D* IRenderTarget3D::getIGraphics3D() const
{
    return static_cast<IGraphics3D*>(getIGraphics2D());
}

void IRenderTarget3D::reset(const Viewport& viewport)
{
    setViewport(viewport);
}

void IRenderTarget3D::translate(const Vector3D& offset)
{
    translate(offset.x, offset.y, offset.z);
}

void IRenderTarget3D::setTextureFilter(TexFilterValue min_filter, TexFilterValue mag_filter)
{
    setTextureFilter(min_filter, mag_filter, 0);
}

void IRenderTarget3D::getTextureFilter(TexFilterValue* min_filter, TexFilterValue* mag_filter)
{
    return getTextureFilter(min_filter, mag_filter, 0);
}

void IRenderTarget3D::setTextureAddressMode(TexAddressMode u_mode, TexAddressMode v_mode)
{
    setTextureAddressMode(u_mode, v_mode, 0);
}

void IRenderTarget3D::getTextureAddressMode(TexAddressMode* u_mode, TexAddressMode* v_mode)
{
    getTextureAddressMode(u_mode, v_mode, 0);
}

bool IRenderTarget3D::fill(const Color& color, const Rect* rect)
{
    Rect r;

    if (!rect)
    {
        r = getTargetRect();
        rect = &r;
    }

    clearBuffer(AL_BUFFER_COLOR|AL_BUFFER_DEPTH, color, rect);
    return true;
}

bool IRenderTarget3D::drawShape(LineShape* line_shape, uint32_t start, uint32_t count)
{
    LineShape3DImpl* shape = restrict_cast<LineShape3DImpl*>(line_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

bool IRenderTarget3D::drawShape(RectangleShape* rectangle_shape, uint32_t start, uint32_t count)
{
    RectangleShape3DImpl* shape = restrict_cast<RectangleShape3DImpl*>(rectangle_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

bool IRenderTarget3D::drawShape(ImageShape* image_shape, uint32_t start, uint32_t count)
{
    ImageShape3DImpl* shape = restrict_cast<ImageShape3DImpl*>(image_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

bool IRenderTarget3D::drawShape(PolylineShape* polyline_shape, uint32_t start, uint32_t count)
{
    PolylineShape3DImpl* shape = restrict_cast<PolylineShape3DImpl*>(polyline_shape);

    if (!shape)
        return false;

    return shape->renderTo(this, start, count);
}

}
